package com.example.onenetdht11;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.onenetdht11.dao.UserDao2;
import com.example.onenetdht11.entity.User2;

public class ChangePasswordActivity extends AppCompatActivity {
    EditText etAccountChange,etOldPassword,etNewPassword,etNewPasswordAgain;
    Button btnAgreeChangePassword;
    ImageButton btnBackRegisterChange;
    User2 user;
    UserDao2 userDao;
    String account,oldPassword,newPassword,agreePassword;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        initView();
        initListener();
    }

    private void initView(){
        etAccountChange = findViewById(R.id.et_account_change);
        etOldPassword = findViewById(R.id.et_old_password);
        etNewPassword = findViewById(R.id.et_new_password);
        etNewPasswordAgain = findViewById(R.id.et_new_password_again);
        btnAgreeChangePassword = findViewById(R.id.btn_agree_changePassword);
        btnBackRegisterChange = findViewById(R.id.btn_back_register_change);
    }

    private void initListener(){
        btnAgreeChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.btn_agree_changePassword){
                    changePassword();
                }
            }
        });
        btnBackRegisterChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.btn_back_register_change){
                    gotoNextActivity(LoginActivity.class);
                }
            }
        });
    }

    private void changePassword() {
        userDao = new UserDao2(this);
        initDate();
        if (userDao.selectByAccount(account,oldPassword)){
            user = userDao.getUserByAccount(account,oldPassword);
            if (newPassword.equals(agreePassword)){
                user.setPassword(newPassword);
                userDao.updatePasswordById(user);
                Toast.makeText(this,"恭喜您修改成功",Toast.LENGTH_SHORT).show();
                gotoNextActivity(LoginActivity.class);
            }else
                Toast.makeText(this,"两次密码输入不一致，请重新输入",Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(this,"用户或密码错误，请重新输入",Toast.LENGTH_SHORT).show();
    }
    private void initDate(){
        account = etAccountChange.getText().toString();
        oldPassword = etOldPassword.getText().toString();
        newPassword = etNewPassword.getText().toString();
        agreePassword = etNewPasswordAgain.getText().toString();
    }

    private void gotoNextActivity(Class activity){
        Intent intent_back = new Intent();
        intent_back.setClass(this,activity);
        startActivity(intent_back);
        finish();
    }
}
